package com.wali.modules.system.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wali.modules.system.dao.ISysMenuDao;
import com.wali.modules.system.vo.SysMenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Wali
 * @ClassName SysMenuService
 * @Description
 * @email
 * @date 2019/4/6
 */
@Service
public class SysMenuService {

    @Autowired
    private ISysMenuDao iSysMenuDao;

    /**
     * @author walixu
     * @MethodName listSysMenu
     * @Description 查询符合条件的角色菜单信息
     * @date 2019/4/6
     */
    public JSONArray listSysMenu(int roleId) {
        SysMenuVO sysMenuVO = new SysMenuVO();
        sysMenuVO.setRoleId(roleId);
        List<SysMenuVO> sysMenuVOList = iSysMenuDao.listRoleSysMenu(sysMenuVO);

        JSONArray menuArray = listChildSysMenu(0, sysMenuVOList);
        return menuArray;
    }

    /**
     * @author walixu
     * @MethodName listChildSysMenu
     * @Description 寻找子菜单
     * @date 2019/4/6
     */
    private JSONArray listChildSysMenu(int parentMenuId, List<SysMenuVO> sysMenuVOList) {
        JSONArray menuArray = new JSONArray();

        for (int i = 0; i < sysMenuVOList.size(); i++) {
            SysMenuVO temp = sysMenuVOList.get(i);
            if (temp.getParentMenuId() == parentMenuId) {// 寻找对应子菜单
                JSONObject menuJSON = new JSONObject();
                menuJSON.put("title", temp.getMenuName());
                menuJSON.put("jump", temp.getHref());
                if (parentMenuId == 0) {
                    menuJSON.put("spread", true);
                }

                JSONArray childMenuArray = listChildSysMenu(temp.getMenuId(), sysMenuVOList);
                if (childMenuArray.size() != 0) {
                    menuJSON.put("list", childMenuArray);
                }

                menuArray.add(menuJSON);
            }
        }

        return menuArray;
    }
}
